'use strict'

const { User, Material, Process, Equipment, WorkOrder } = require('../../backend/src/models')
const bcrypt = require('bcryptjs')

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // 创建管理员用户
    await User.create({
      username: 'admin',
      password: '123456',
      name: '管理员',
      email: 'admin@company.com',
      role: 'admin'
    })

    // 创建物料数据
    const materials = await Material.bulkCreate([
      {
        code: 'MAT001',
        name: '原材料A',
        specification: '规格1',
        unit: 'kg',
        category: '原材料',
        type: 'raw',
        currentStock: 1000,
        safeStock: 200,
        price: 10.5
      },
      {
        code: 'MAT002',
        name: '半成品B',
        specification: '规格2',
        unit: '件',
        category: '半成品',
        type: 'semi-finished',
        currentStock: 500,
        safeStock: 100,
        price: 25.0
      },
      {
        code: 'MAT003',
        name: '成品C',
        specification: '规格3',
        unit: '件',
        category: '成品',
        type: 'finished',
        currentStock: 200,
        safeStock: 50,
        price: 100.0
      }
    ])

    // 创建工艺数据
    const processes = await Process.bulkCreate([
      {
        code: 'PROC001',
        name: '加工工艺1',
        type: 'machining',
        standardTime: 60,
        description: '基础加工工艺'
      },
      {
        code: 'PROC002',
        name: '装配工艺1',
        type: 'assembly',
        standardTime: 30,
        description: '产品装配工艺'
      }
    ])

    // 创建设备数据
    const equipment = await Equipment.bulkCreate([
      {
        code: 'EQ001',
        name: '加工中心1',
        type: 'CNC',
        model: 'XYZ-1000',
        status: 'idle',
        location: '车间A'
      },
      {
        code: 'EQ002',
        name: '装配线1',
        type: 'Assembly',
        model: 'ASM-500',
        status: 'idle',
        location: '车间B'
      }
    ])

    // 创建工单数据
    await WorkOrder.bulkCreate([
      {
        workOrderNo: 'WO2024001',
        productId: materials[2].id,
        productName: '成品C',
        planQuantity: 100,
        status: 'pending',
        processId: processes[1].id,
        equipmentId: equipment[1].id
      },
      {
        workOrderNo: 'WO2024002',
        productId: materials[1].id,
        productName: '半成品B',
        planQuantity: 200,
        status: 'running',
        processId: processes[0].id,
        equipmentId: equipment[0].id,
        startTime: new Date()
      }
    ])
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.bulkDelete('work_orders', null, {})
    await queryInterface.bulkDelete('equipment', null, {})
    await queryInterface.bulkDelete('processes', null, {})
    await queryInterface.bulkDelete('materials', null, {})
    await queryInterface.bulkDelete('users', null, {})
  }
}
